#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

int findUnsortedSubarray(vector<int>& nums) {
    if (is_sorted(nums.begin(), nums.end()))  return 0;
    vector<int> t(nums);
    sort(nums.begin(), nums.end());
    int l = 0;
    while (nums[l] == t[l])   l++;
    int r = nums.size() - 1;
    while (nums[r] == t[r])   r--;
    return r - l + 1;
}